package com.summer_administration.controller;

import com.summer_administration.mapper.GroupeMapper;
import com.summer_administration.pojo.CommuneArrange;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Result;
import com.summer_administration.service.CommuneArrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/communeArrange")
@Validated
public class CommuneArrangeController {
    @Autowired
    private CommuneArrangeService communeArrangeService;

    @Autowired
    private GroupeMapper groupeMapper;

    @PostMapping
    public Result add(@RequestBody @Validated CommuneArrange arrange) {
        // 新增一个根剧小组名字查询小组id
        String groupeName = arrange.getGroupName();
        String groupId = groupeMapper.findGroupIdByGroupName(groupeName);
        arrange.setGroupId(groupId);
        communeArrangeService.add(arrange);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<CommuneArrange>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        PageBean<CommuneArrange> pb = communeArrangeService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated CommuneArrange arrange) {
        communeArrangeService.update(arrange);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        communeArrangeService.delete(id);
        return Result.success();
    }

    @GetMapping("/getListByGroupId")
    public Result<List<CommuneArrange>> getByGroupId(@RequestParam String groupId) {
        List<CommuneArrange> list = communeArrangeService.getByGroupId(groupId);
        return Result.success(list);
    }

    @GetMapping("/getConflictList")
    public Result<String> checkConflict(@RequestParam String groupId) {
        String conflict = communeArrangeService.checkConflict(groupId);
        return Result.success(conflict);
    }
}
